class Solution {
public:
    vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
        vector<vector<int>>window(n);
        for(int i = 0;i < n- 1;i++) 
            window[i].push_back(i + 1);
        auto bfs =[&]() ->int{
            vector<int> dist(n,-1);
            queue<int> q;
            q.push(0);
            dist[0] = 0;
            while(!q.empty())
            {
                int x = q.front();
                q.pop();
                for(int y : window[x])
                {
                    if(dist[y] == -1)
                    {
                        q.push(y);
                        dist[y] = dist[x] + 1;
                    }
                }
            }
            return dist[n - 1];
        };


        vector<int> ans(queries.size());
        for (int i = 0; i < queries.size(); i++) {
            window[queries[i][0]].push_back(queries[i][1]);
            ans[i] = bfs();
        }
        return ans;
    }
};